package com.taoye.thread;

import cn.hutool.core.date.DateUtil;

/**
 * 设置wait等待时间
 *
 * @author : Mr.Tao
 * @version : 1.0
 * @date : 2022/4/4 10:31
 **/
public class Demo41 {
    public static void main(String[] args) throws InterruptedException {
        Object lock = new Object();
        Thread t = new Demo41ThreadA(lock);
        t.setName("A");
        t.start();
        Thread.sleep(5000);
        Thread t2 = new Demo41ThreadB(lock);
        t2.setName("唤醒线程");
        t2.start();
    }
}

class Demo41ThreadA extends Thread {
    private Object lock;

    public Demo41ThreadA(Object lock) {
        this.lock = lock;
    }

    @Override
    public void run() {
        synchronized (lock) {
            try {
                System.out.println("我是线程" + Thread.currentThread().getName() + "，现在开始等待3秒，时间：" + DateUtil.now());
                lock.wait(3000);
                System.out.println("我是线程" + Thread.currentThread().getName() + "，现在等待结束了，时间：" + DateUtil.now());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class Demo41ThreadB extends Thread {
    private Object lock;

    public Demo41ThreadB(Object lock) {
        this.lock = lock;
    }

    @Override
    public void run() {
        synchronized (lock) {
            System.out.println("我是唤醒线程，现在开始唤醒，时间：" + DateUtil.now());
            lock.notify();
            System.out.println("我是唤醒线程，唤醒完成，时间：" + DateUtil.now());
        }
    }
}